<template>
  <el-dialog class="edit-password" title="旅游产品分类" append-to-body :close-on-click-modal="closeOnclickModal"
             width="800px" top="3.8vh" :visible.sync="isDialogShow" @close="close">
    <el-form :model="dialogForm" ref="dialogForm" :size="size" label-width="100px">
      <el-form-item :rules="yh_rules({required:true})" label="分类名称" prop="itemValue">
        <el-input v-model="dialogForm.itemValue" placeholder="请输入分类名称" clearable />
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="close" :size="size">取消</el-button>
      <el-button type="primary" @click="submit" :size="size">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {mapGetters} from 'vuex'
  import api from '@/utils/api.js'
  export default {
    name: 'EditTypeModal',
    props: {
      typeDialogShow: {
        type: Boolean,
        default: false
      },
      propTypeInfo: {
        type: Object
      }
    },
    data () {
      return {
        isDialogShow: false,
        dialogForm: {
          itemValue: ''
        }
      }
    },
    computed: {
      ...mapGetters(['size', 'closeOnclickModal']),
    },
    watch: {
      typeDialogShow (newValue) {
        this.isDialogShow = newValue
        if (newValue && this.propTypeInfo.itemKey) {
          Object.assign(this.dialogForm, this.propTypeInfo)
        }
      }
    },
    methods: {
      // 提交
      submit () {
        let apiFunc
        if (!this.dialogForm.itemKey) {
          apiFunc = api.addTravelType
        } else {
          apiFunc = api.updateTravelType
        }
        this.$validateSubmit('dialogForm').then(() => {
          apiFunc(this.dialogForm).then(res => {
            this.$message.success('操作成功')
            this.$emit('refreshData')
            this.close()
          })
        })
      },
      close () {
        this.dialogForm = {
          itemKey: '',
          itemValue: ''
        }
        this.$emit('update:typeDialogShow', false)
      },
    }
  }
</script>
